<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\User;
use Validator;
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
    	$data = DB::table('users')->paginate(1);
    	//第一种方法
       // return view('admin.user_list',['data'=>$data]);
        //第二种方法
        return view('admin.user_list')->with('data',$data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.user_add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {	
    	
     	$messages = [
    	'required'    		=>  ' :attribute  是必填字典',
    	'email'    		        =>  ' :attribute  邮箱输入错误',
    	'min'      				=>  ' :attribute  密码6 位',
    	'mobile'      		=>  ' :attribute  手机号必须11 位',
    	];
    	
        $data = Validator::make($request->all(), [
            'name' 		=>  'required|max:20',
            'email' 		=>  'required|max:30',
            'password' =>  'required|min:4',
        	'mobile'      =>  'required',
        ],$messages);
	   
    	 if ($data->fails()){
    	 	return back()->withErrors("填写错误，请从新填写");
    	 }
    	$user['name'] = $request->name;
    	$user['mobile']=$request->mobile;
    	$user['email']=$request->email; 
    	$user['password']=bcrypt($request->password); 
       	
    	DB::table('users')->insert($user);
        
        return redirect('/admin/user');
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = DB::table('users')->where('id',$id)->first();
       return view('admin.user_show',['data'=>$data]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {	
    	$data = User::find($id);
        return view('admin.user_edit')->with('data',$data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
         $data = DB::table('users')->where('id','=',$id)->update([
         		'name'=>$request->name,
         		'password'=>$request->password,
         		'mobile'=>$request->mobile,
         		'email'=>$request->email,
         		]);
         return redirect('admin/user');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
